package com.lz.ub.core

import com.lz.ub.*

/**
 * @author You
 * UBC取消自动重连, 取消自动重连并断开
 */
internal class UBCAutoCancel(private val operator: UBCOperator) : UBCWritable {

    //当前指令
    private var currentCommand: String? = null

    fun cancel() {
        currentCommand = COMMAND_DISCONNECT
        operator.write(currentCommand!!, this)
    }

    override val label: String = "AutoCancel"

    override fun responseOk() {
        operator.log("disconnect ok callback and command = $currentCommand")
        when (currentCommand) {
            COMMAND_DISCONNECT ->{
                Thread.sleep(COMMAND_CONNECTED_DELAYED)
                currentCommand = COMMAND_CANCEL_AUTO
                operator.write(currentCommand!!, this)
            }
            COMMAND_CANCEL_AUTO -> {
                currentCommand = null
                operator.disconnectOver()
            }
        }
    }

    override fun responseFail() {
        //nothing
    }

    override fun close() {
        currentCommand = null
    }

    override fun isAvailable(): Boolean = currentCommand == null
}